<div id="header"><div>Your Account</div></div>
<div id="subheader"><div>SMS Notifications</div></div>
<?php

	// If we need to save the phone number
	if($_GET['action'] === "setsms")
	{
			// Parse phone number from input
			$input = str_replace(array(' ','-',')','('), '', $_POST['phoneno']);

			// If valid phone number
			if (preg_match('/(\d{3})(\d{3})(\d{4})/', $input, $output))
			{
				// Was the user trying to add a phone number?
				if($_POST['submit'] === 'Add')
				{	
					// Spit out the confirmation form
					print_sms_confirm_form();

					// Store session vars for the confirmation code and number and provider
					$_SESSION['sms_confirm_code'] = rand(100000,999999);
					$_SESSION['sms_number'] = $output[0];
					$_SESSION['sms_provider'] = $_POST['provider'];

					// Send the user a confirmation code
					sms_confirm_code($output[0], $_POST['provider'], $_SESSION['sms_confirm_code']);
				}
				// Was the user trying to remove a phone number?
				else if($_POST['submit'] === 'Remove')
				{
					// Unregister the phone number
					// TODO: use confirmation for unregistering too
					unregister_phone_number($output[0], $_POST['provider']);
					echo '<p>Phone number removed.</p>';

					// Spit out standard sms form
					print_sms_form();
				}
			
			} 
			else 
			{
				// The phone number was invalid, tell the user and print the form again
				echo '<p>Invalid phone number.  Please include your entire number, including area code.</p>';
				print_sms_form();
			}
	}

	// Confirmation code was entered
	else if($_GET['action'] === "confirm")
	{
		// Ensure the session var and the code entered match
		if($_SESSION['sms_confirm_code'] == $_POST['code'])
		{

			// Register phone and inform teh user
			register_phone_number($_SESSION['sms_number'],$_SESSION['sms_provider']);
			echo '<p>Your phone has been successfully registered!</p>';
		}
		else
		{
			// User failed to  enter confirmation code
			echo '<p>Invalid confirmation code.</p>';
		}

		// Unset the temporary variables
		unset($_SESSION['sms_confirm_code']);
		unset($_SESSION['sms_number']);
		unset($_SESSION['sms_provider']);

		// Print out the sms form
		print_sms_form();
	}
	else
	{
		echo '<p> To receive SMS alerts about unusual room activity, enter your phone number and service provider in the form below. Please include your area code when entering your phone number</p>
			<p><b>Standard text messaging rates apply</b></p>';
		print_sms_form();
	}

function print_sms_confirm_form()
{
	echo '<div>
	<p>A confirmation code has been sent to your phone.  Please enter it in the form below to confirm your identity.</p>
	<form name="input" action="?p=settings&action=confirm" method="post">
	<label for="code">Code:</label>
	<input size="25" type="text" style="font-family:monospace" name="code"/>
	<p class="submit"><input type="submit" value="Confirm" name="submit"/></p>
	</form>
	</div>';

}

function print_sms_form()
{
	echo '<div>
	<form name="input" action="?p=settings&action=setsms" method="post">
	<label for="phoneno">Number:</label>
	<input size="25" type="text" style="font-family:monospace" name="phoneno"/>
	<br />
	<label for="provider" >Service:</label>
	<select name="provider" style="font-family:monospace">
	<option value="verizon">Verizon</option>
	<option value="tmobile">T-Mobile</option>
	<option value="sprint">Sprint</option>
	<option value="att">AT&T</option>
	</select>
	<p class="submit"><input type="submit" value="Remove" name="submit"/></p>
	<p class="submit"><input type="submit" value="Add" name="submit"/></p>
	</form>
	</div>';
}
?>

